import SwiftUI
import SPGlassKit

public struct GlassNavigationBarExample: GlassExample {
    public static let componentID = "glass-navigation-bar"
    @State private var notes = ["Quarterly review", "Trip to Lisbon", "A much longer note title that has to wrap or truncate gracefully"]
    @State private var isFlagged = false

    public init() {}

    public var body: some View {
        GlassNavigationBar(
            "Notes",
            subtitle: "\(notes.count) notes",
            titleDisplay: .large,
            leading: [
                GlassToolbarAction(id: "sort", title: "Sort", systemImage: "arrow.up.arrow.down") { notes.sort() },
            ],
            trailing: [
                GlassToolbarAction(id: "new", title: "New note", systemImage: "square.and.pencil", shortcut: KeyboardShortcut("n"), priority: .high) {
                    notes.append("Untitled \(notes.count + 1)")
                },
                GlassToolbarAction(id: "shuffle", title: "Shuffle", systemImage: "shuffle") { notes.shuffle() },
                GlassToolbarAction(id: "clear", title: "Delete all", systemImage: "trash", role: .destructive, isEnabled: !notes.isEmpty, priority: .low) {
                    notes.removeAll()
                },
            ]
        ) {
            List(notes, id: \.self) { note in
                NavigationLink(note, value: note)
            }
            .overlay {
                if notes.isEmpty { ContentUnavailableView("No notes", systemImage: "note.text") }
            }
            .navigationDestination(for: String.self) { note in
                ScrollView {
                    Text(verbatim: note)
                        .font(.title3)
                        .frame(maxWidth: .infinity, alignment: .leading)
                        .padding()
                }
                .glassNavigationBar(
                    "Note",
                    titleDisplay: .inline,
                    trailing: [
                        GlassToolbarAction(id: "flag", title: isFlagged ? "Remove flag" : "Flag", systemImage: isFlagged ? "flag.fill" : "flag") {
                            isFlagged.toggle()
                        },
                    ]
                )
            }
        }
    }
}

#Preview("GlassNavigationBar") { GlassNavigationBarExample().glassTheme(.ocean) }
